﻿using AutoMapper;
using B.S.BaseData.Domain.Entities;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.Read.API.Command.UserCommand;
using MediatR;

namespace B.S.BaseData.Read.API.CommandHandler.UserHandler
{
    public class LoginUserHandler : IRequestHandler<LoginUserCommand, APIResult<User>>
    {
        private readonly IBaseRepository<User> userRep;
        private readonly IMapper mapper;

        public LoginUserHandler(IBaseRepository<User> userRep,IMapper mapper)
        {
            this.userRep = userRep;
            this.mapper = mapper;
        }
        public Task<APIResult<User>> Handle(LoginUserCommand request, CancellationToken cancellationToken)
        {
            APIResult<User> result = new APIResult<User>();
            var user = userRep.GetAll().Where(x=>x.Name==request.Name).FirstOrDefault();
            if (user == null) 
            {
                result.Code=APIEnum.用户名不存在;
                result.Msg=APIEnum.用户名不存在.ToString();
              
            }
            else
            {
                if (user.Pass!=request.Pass)
                {
                    result.Code = APIEnum.用户名和密码不匹配;
                    result.Msg = APIEnum.用户名和密码不匹配.ToString();
                }
                else 
                {
                    result.Code = APIEnum.登陆成功;
                    result.Msg = APIEnum.登陆成功.ToString();
                    result.Data = user;
                   
                }
            }

            return Task.FromResult(result);
        }
    }
}
